package com.ar.customview;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * The Class SubCategory.
 * @author anh
 */
public class SubCategory extends LinearLayout {
	
	/** The data. */
	private JSONObject data;
	
	/** The subject id. */
	private String subjectId;
	
	/** The subject name. */
	private String subjectName;
	
	/** The image name. */
	private String imageName;
	
	/** The m context. */
	private Context mContext;
	
	/** The tv subject name. */
	private TextView tvSubjectName;
	
	/** The img view subject icon. */
	private ImageView imgViewSubjectIcon;

	/**
	 * Instantiates a new sub category.
	 *
	 * @param context the context
	 */
	public SubCategory(Context context) {
		super(context);
		
		this.mContext = context;
		
		this.setOrientation(VERTICAL);
		this.setGravity(Gravity.CENTER);
		
		imgViewSubjectIcon = new ImageView(context);
		imgViewSubjectIcon.setAdjustViewBounds(true);
		imgViewSubjectIcon.setScaleType(ImageView.ScaleType.CENTER_CROP);
		imgViewSubjectIcon.setBackgroundColor(Color.TRANSPARENT);
		addView(imgViewSubjectIcon, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		tvSubjectName = new TextView(context);
		addView(tvSubjectName, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}

	/**
	 * Instantiates a new sub category.
	 *
	 * @param context the context
	 * @param attrs the attrs
	 */
	public SubCategory(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Instantiates a new sub category.
	 *
	 * @param context the context
	 * @param attrs the attrs
	 * @param defStyle the def style
	 */
	@SuppressLint("NewApi")
	public SubCategory(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	

	/**
	 * Gets the data.
	 *
	 * @return the data
	 */
	public JSONObject getData() {
		return data;
	}

	/**
	 * Sets the data.
	 *
	 * @param data the new data
	 */
	public void setData(JSONObject data) {
		this.data = data;
		try {
			setSubjectId(data.getString("id"));
			setSubjectName(data.getString("name"));
			setImageName(data.getString("image"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	

	/**
	 * Gets the subject id.
	 *
	 * @return the subject id
	 */
	public String getSubjectId() {
		return subjectId;
	}

	/**
	 * Sets the subject id.
	 *
	 * @param subjectId the new subject id
	 */
	public void setSubjectId(String subjectId) {
		this.subjectId = subjectId;
	}
	

	/**
	 * Gets the subject name.
	 *
	 * @return the subject name
	 */
	public String getSubjectName() {
		return subjectName;
	}

	/**
	 * Sets the subject name.
	 *
	 * @param subjectName the new subject name
	 */
	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
		tvSubjectName.setText(subjectName);
	}
	

	/**
	 * Gets the image name.
	 *
	 * @return the image name
	 */
	public String getImageName() {
		return imageName;
	}

	/**
	 * Sets the image name.
	 *
	 * @param imageName the new image name
	 */
	public void setImageName(String imageName) {
		if(!(imageName.equals("")||imageName == null)) {
			this.imageName = imageName;
		imgViewSubjectIcon.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(imageName, "drawable", mContext.getPackageName())));
		}
	}

}
